#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QGridLayout>

#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // QGridLayout
    /*
    QPushButton* button1 = new QPushButton("按钮1");
    QPushButton* button2 = new QPushButton("按钮2");
    QPushButton* button3 = new QPushButton("按钮3");
    QPushButton* button4 = new QPushButton("按钮4");

    QGridLayout* layout = new QGridLayout();*/
    /*
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 0, 1);
    layout->addWidget(button3, 1, 0);
    layout->addWidget(button4, 1, 1);
    */

    /*
    // 这个写法就相当于水平布局了
    // 此时大家的 行数 只要是一样的即可, 不一定非得是 0
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 0, 1);
    layout->addWidget(button3, 0, 2);
    layout->addWidget(button4, 0, 3);*/

    /*
    // 这个写法就相当于垂直布局了
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 1, 0);
    layout->addWidget(button3, 2, 0);
    layout->addWidget(button4, 3, 0);*/

    /*
    // 这种写法是每个按钮独占一行和一列
    // 即使设置成 100, 100, 也不会在中间搞出很大的空白空间.
    // 此处设置的行数和列数, 只是用来决定控件之间的相对位置.
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 1, 1);
    layout->addWidget(button3, 2, 2);
    layout->addWidget(button4, 100, 100);*/

    // QGridLayout2
    // 创建 6 个按钮, 使用网格布局按照 2 * 3 的方式来排列
    /*
    QPushButton* button1 = new QPushButton("按钮1");
    QPushButton* button2 = new QPushButton("按钮2");
    QPushButton* button3 = new QPushButton("按钮3");
    QPushButton* button4 = new QPushButton("按钮4");
    QPushButton* button5 = new QPushButton("按钮5");
    QPushButton* button6 = new QPushButton("按钮6");

    // 创建网格布局管理器, 把这些控件添加进去
    QGridLayout* layout = new QGridLayout();
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 0, 1);
    layout->addWidget(button3, 0, 2);
    layout->addWidget(button4, 1, 0);
    layout->addWidget(button5, 1, 1);
    layout->addWidget(button6, 1, 2);

    this->setLayout(layout);

     // 设置水平拉伸系数.
     layout->setColumnStretch(0, 0);
     layout->setColumnStretch(1, 1);
     layout->setColumnStretch(2, 2);

     this->setLayout(layout);
      */

    //QFormLayout
    /*
    // 设置成 3 行 2 列.
    QFormLayout* layout = new QFormLayout();
    this->setLayout(layout);

    // 创建 3 个 label 作为第一列
    QLabel* label1 = new QLabel("姓名");
    QLabel* label2 = new QLabel("年龄");
    QLabel* label3 = new QLabel("电话");

    // 创建 3 个 输入框 作为第二列
    QLineEdit* edit1 = new QLineEdit();
    QLineEdit* edit2 = new QLineEdit();
    QLineEdit* edit3 = new QLineEdit();

    // 把上述控件添加到表单布局中
    layout->addRow(label1, edit1);
    layout->addRow(label2, edit2);
    layout->addRow(label3, edit3);

    // 创建一个 "提交按钮"
    QPushButton* button = new QPushButton("提交");
    layout->addRow(nullptr, button);*/


    // QSpacerItem
    QHBoxLayout* layout = new QHBoxLayout();
    this->setLayout(layout);

    QPushButton* button1 = new QPushButton("按钮1");
    QPushButton* button2 = new QPushButton("按钮2");

    // 创建 spacer 使两个按钮之间存在空白.
    QSpacerItem* spacer = new QSpacerItem(200, 20);

    // 当前是要把空白添加到两个按钮之间. 此处 add 的顺序就是把 addSpacerItem 放到中间了.
    layout->addWidget(button1);
    layout->addSpacerItem(spacer);
    layout->addWidget(button2);


}

Widget::~Widget()
{
    delete ui;
}

